package com.liuweidihuangwan.jingdong.service.impl;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuweidihuangwan.jingdong.common.BaseResponse;
import com.liuweidihuangwan.jingdong.common.ResultUtils;
import com.liuweidihuangwan.jingdong.constant.JwtClaimsConstant;
import com.liuweidihuangwan.jingdong.exception.BusinessException;
import com.liuweidihuangwan.jingdong.exception.ErrorCode;
import com.liuweidihuangwan.jingdong.model.dto.user.UserLoginRequest;
import com.liuweidihuangwan.jingdong.model.vo.UserVO;
import com.liuweidihuangwan.jingdong.properties.JwtProperties;
import com.liuweidihuangwan.jingdong.service.UserinfoService;
import com.liuweidihuangwan.jingdong.model.entity.Userinfo;
import com.liuweidihuangwan.jingdong.mapper.UserinfoMapper;
import com.liuweidihuangwan.jingdong.utils.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【userinfo】的数据库操作Service实现
* @createDate 2025-01-08 11:48:25
*/
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo>
    implements UserinfoService {

    @Resource
    JwtProperties jwtProperties;

    /**
     * 用户登录
     * @param userLoginRequest
     * @return
     */
    @Override
    public BaseResponse<UserVO> userLogin(UserLoginRequest userLoginRequest) {
        //参数校验
        if(userLoginRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"传入参数为空");
        }
        String phone = userLoginRequest.getPhone();
        String password = userLoginRequest.getPassword();
        //密码加密
        password = DigestUtil.md5Hex(password);

        if(StrUtil.isEmpty(phone) || StrUtil.isEmpty(password)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"手机号或密码为空");
        }
        //查询数据库
        boolean isLogin = true;
        Userinfo userinfo = lambdaQuery()
                .eq(Userinfo::getPhone, phone)
                .one();
        if(userinfo == null){
            //此时注册
            userinfo = new Userinfo();
            userinfo.setImg("https://img11.360buyimg.com/jdphoto/s120x120_jfs/t21160/90/706848746/2813/d1060df5/5b163ef9N4a3d7aa6.png");
            userinfo.setUserName("无名");
            userinfo.setPhone(phone);
            userinfo.setPassword(password);
            //添加用户信息
            save(userinfo);
            isLogin = false;
        }
        //检查密码是否正确
        if(!password.equals(userinfo.getPassword())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"密码错误");
        }
        //将用户信息转化成token
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, userinfo.getUserID());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        //封装返回数据
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userinfo,userVO);
        userVO.setToken(token);
        userVO.setLogin(isLogin);
        return ResultUtils.success(userVO);
    }


}




